package com.sara.service.impl;

import com.sara.entity.*;
import com.sara.exception.ServiceException;
import com.sara.mapper.EmployeeRoleMapper;
import com.sara.mapper.PermissionMapper;
import com.sara.mapper.RoleMapper;
import com.sara.mapper.RolePermissionMapper;
import com.sara.service.PermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class permissionserviceImpl implements PermissionService {
    @Autowired   
    private EmployeeRoleMapper employeeRoleMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    /**
     * 通过员工id查找角色
     *
     * @param id 员工id
     * @return 角色对象
     */
    @Override
    public List<Role> findRoleByEmployeeId(Integer id) {
        
        List<Role> roleList = new ArrayList<>();
        if(StringUtils.isNumeric(Integer.toString(id))){
            EmployeeRoleExample employeeRoleExample = new EmployeeRoleExample();
            employeeRoleExample.createCriteria().andEmployeeIdEqualTo(id);
            List<EmployeeRole> employeeRoles = employeeRoleMapper.selectByExample(employeeRoleExample);
            for(EmployeeRole employeeRole : employeeRoles){
               Role role = roleMapper.selectByPrimaryKey(employeeRole.getRoleId());
               roleList.add(role);  
            }
        }
        return roleList;
    }

    /**
     * 查找所有权限
     *
     * @return 返回权限集合
     */
    @Override
    public List<Permission> findAllPermission() {
        // return permissionMapper.selectByExample(new PermissionExample());
        List<Permission> permissionList = permissionMapper.selectByExample(new PermissionExample());
        List<Permission> endList = new ArrayList<>();
        treeList(permissionList,endList,0);
        return endList;
    }

    private void treeList(List<Permission> permissionList, List<Permission> endList, int pid) {
        List<Permission> tempList = new ArrayList<>();
        tempList.addAll(permissionList);

        for(Permission permission : tempList){
            if(permission.getPid() == pid){
                endList.add(permission);
                treeList(tempList, endList, permission.getId());
                //System.out.println("递归出来的依次是：" + permission.getPermissionName());
            }
        }

/*
        for(Permission permission : permissionList){
            if(permission.getPid() == pid){
                endList.add(permission);
                pid = permission.getId();
                treeList(permissionList,endList,pid);
                System.out.println("递归出来的依次是：" + permission.getPermissionName());
            }

        }*/
    }
    /**
     * 通过角色id查找权限集合
     *
     * @param id 角色id
     * @return 权限集合
     */
    @Override
    public List<Permission> findPermissionByRoleId(Integer id) {
        List<Permission> permissionList = new ArrayList<>();
        RolePermissionExample rolePermissionExample = new RolePermissionExample();
        rolePermissionExample.createCriteria().andRoleIdEqualTo(id);
        List<RolePermission> rolePermissions = rolePermissionMapper.selectByExample(rolePermissionExample);
        
        for(RolePermission rolePermission : rolePermissions){
            Permission permission = permissionMapper.selectByPrimaryKey(rolePermission.getPermissionId());
            permissionList.add(permission);
        }
        return permissionList;
    }

    @Override
    public List<Permission> findPermissionByType(String permissionTypeMenu) {
        PermissionExample permissionExample = new PermissionExample();
        permissionExample.createCriteria().andPermissionTypeEqualTo(permissionTypeMenu);
        return permissionMapper.selectByExample(permissionExample);
    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insertSelective(permission);
    }

    @Override
    public Permission findPermissionById(Integer id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    /**
     * 把当前权限的下级所有权限递归出来到集合里面返回到前端，
     *
     * @param id
     * @return
     */
    @Override
    public List<Permission> findPermissionWithPid(Integer id) {
        List<Permission> endList = new ArrayList<>();
        findWithPid(endList,id);
        return endList;
    }

    @Override
    public void edit(Permission permission) {
        permissionMapper.updateByPrimaryKeySelective(permission);
    }

    /**
     * 根据id删除权限
     *
     * @param id
     */
    @Override
    public void delete(Integer id) throws ServiceException {
        RolePermissionExample rolePermissionExample = new RolePermissionExample();
        rolePermissionExample.createCriteria().andPermissionIdEqualTo(id);
        List<RolePermission> rolePermissionList = rolePermissionMapper.selectByExample(rolePermissionExample);

        if(rolePermissionList != null && rolePermissionList.size() > 0){
            throw new ServiceException("请移除该权限下的所有角色,再删除!");
        }
        permissionMapper.deleteByPrimaryKey(id);

        PermissionExample permissionExample = new PermissionExample();
        permissionExample.createCriteria().andPidEqualTo(id);
        List<Permission> permissionList = permissionMapper.selectByExample(permissionExample);
        if(permissionList != null && permissionList.size() > 0){
            throw new ServiceException("请移除该权限下的所有子权限,再删除!");
        }
        permissionMapper.deleteByPrimaryKey(id);

    }

    private void findWithPid(List<Permission> endList, Integer id) {
        PermissionExample permissionExample = new PermissionExample();
        permissionExample.createCriteria().andPidEqualTo(id);
        List<Permission> permissionList = permissionMapper.selectByExample(permissionExample);

        if(permissionList.size() > 0){
            for(Permission permission : permissionList){
                endList.add(permission);
                findWithPid(endList,permission.getId());
            }
        }
    }

}
